package sg.edu.nus.iss;

import java.awt.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.*;

/**
 * 
 * @author Lem Kian Hoa
 *
 */

public class GAChart extends ApplicationFrame {
    
	private static final long serialVersionUID = 1L;
	private static final String CHART_BEST_FITNESS = "Best";
	private static final String CHART_AVERAGE_FITNESS = "Average";
	private static final String CHART_AXIS_X = "Generation";
	private static final String CHART_AXIS_Y = "Fitness Value";
	private static final String CHART_NAME = "Load Package Distribution";
	private static final String CHART_TITLE = "Genetic Algorithm Chart";
	
	private XYSeries bestFitnessChart = new XYSeries(CHART_BEST_FITNESS);
	private XYSeries averageFitnessChart = new XYSeries(CHART_AVERAGE_FITNESS);

	private static class GAChartPanel extends JPanel {
		private static final long serialVersionUID = 1L;
		private XYDataset data;
		private List<JFreeChart> charts;

		private ChartPanel createChartPanel() {
			NumberAxis numberaxis = new NumberAxis(CHART_AXIS_X);
			numberaxis.setAutoRangeIncludesZero(false);
			NumberAxis numberaxis1 = new NumberAxis(CHART_AXIS_Y);
			numberaxis1.setAutoRangeIncludesZero(false);
			XYPlot xyplot = new XYPlot(data, numberaxis, numberaxis1, new StandardXYItemRenderer());
			xyplot.setBackgroundPaint(Color.lightGray);
			xyplot.setDomainGridlinePaint(Color.white);
			xyplot.setRangeGridlinePaint(Color.white);
			xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
			JFreeChart jfreechart = new JFreeChart(CHART_NAME, JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
			charts.add(jfreechart);
			ChartUtilities.applyCurrentTheme(jfreechart);
			
			try {
				ChartUtilities.saveChartAsJPEG(new java.io.File(Main.outputPath + ".jpg"), jfreechart, 800, 600);
			} catch (IOException ex) {
				throw new RuntimeException(ex);
			}

			ChartPanel chartpanel = new ChartPanel(jfreechart);
			return chartpanel;
		}

		public GAChartPanel(XYDataset data) {
			super(new BorderLayout());
	        charts = new ArrayList<JFreeChart>();
			this.data = data;
			add(createChartPanel());
		}
	}

	public GAChart() {
		super(CHART_TITLE);
	}
	
	public void addBestFitnessSeries(double x, double y) {
		bestFitnessChart.add(x,y);
	}

	public void addAverageFitnessSeries(double x, double y) {
		averageFitnessChart.add(x,y);
	}

	public void display() {
		XYSeriesCollection col = new XYSeriesCollection(bestFitnessChart);
		col.addSeries(averageFitnessChart);
		JPanel jpanel = new GAChartPanel(col);
		getContentPane().add(jpanel);
		pack();
		RefineryUtilities.centerFrameOnScreen(this);
		setVisible(true);
	}
}
